package com.xiumu.common.core.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.Getter;

import java.util.stream.Stream;

/**
 * 文章状态
 *
 * @Author XiuMu
 * @Date 2023/3/10 21:39
 **/
@Getter
public enum PostStatus implements BaseEnum {

    DRAFT(0, "草稿"),
    PUBLISHED(100, "已发布"),
    ;

    PostStatus(Integer code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    @JsonValue
    @EnumValue
    private final Integer code;

    private final String desc;

    /**
     * 根据 code 反序列化
     * 如果没有就返回 null
     *
     * @param code code
     * @return
     */
    @JsonCreator
    public static PostStatus codeOf(Integer code) {
        return Stream.of(PostStatus.class.getEnumConstants())
                .filter(item -> item.code.equals(code))
                .findFirst()
                .orElse(null);
    }
}
